﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TLECDataLayer
{
    public class Locations
    {
        public static void CreateLocation(string loc)
        {
            if (!string.IsNullOrEmpty(loc))
            {
                TLECDataDataContext context = new TLECDataDataContext();

                var res =
                    (from l in context.Locations
                     where l.LocationName == loc
                     select l).FirstOrDefault();

                if (res == null)
                {
                    Location l = new Location()
                    {
                        LocationName = loc
                    };

                    context.Locations.InsertOnSubmit(l);
                    context.SubmitChanges();
                }
            }
            else
            {
                throw new ArgumentException("Invalid location name");
            }

        }

        public static void UpdateLocation(int locId, string loc)
        {
            if (!string.IsNullOrEmpty(loc))
            {
                TLECDataDataContext context = new TLECDataDataContext();

                var res =
                    (from l in context.Locations
                     where l.LocationId == locId
                     select l).FirstOrDefault();

                if (res != null)
                {
                    res.LocationName = loc;
                    context.SubmitChanges();
                }
                else
                {
                    throw new ArgumentException("Invalid location id");
                }
            }
            else
            {
                throw new ArgumentException("Invalid location name");
            }
        }

        public static void DeleteLocation(int locId)
        {
            TLECDataDataContext context = new TLECDataDataContext();

            var res =
                (from l in context.Locations
                 where l.LocationId == locId
                 select l).FirstOrDefault();

            if (res != null)
            {
                if (Events.GetAllEventsByLocation(locId).Count() == 0)
                {
                    context.Locations.DeleteOnSubmit(res);
                    context.SubmitChanges();
                }
            }
            else
            {
                throw new ArgumentException("Invalid location id");
            }
        }

        public static IQueryable<Location> GetAllLocations()
        {
            TLECDataDataContext context = new TLECDataDataContext();

            return context.Locations;
        }

        public static Location GetLocationById(int locId)
        {
            TLECDataDataContext context = new TLECDataDataContext();

            var res =
                (from l in context.Locations
                 where l.LocationId == locId
                 select l).FirstOrDefault();

            return res;
        }

    }
}
